package org.paul.gotobplan;

import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import org.androidannotations.annotations.*;
import org.androidannotations.annotations.sharedpreferences.Pref;
import org.paul.gotobplan.bean.LoginBean;
import org.paul.gotobplan.manager.PrefsManager_;
import org.paul.gotobplan.ui.HomeAct_;
import org.paul.gotobplan.ui.ProfileAct_;
import org.paul.gotobplan.ui.SignOutAct_;

import java.util.HashMap;
import java.util.Map;


@EActivity(R.layout.activity_main)
public class MainActivity extends BaseAct {

    @Pref
    PrefsManager_ prefsManager;

    @ViewById
    TextView tv;
    @ViewById
    ListView listView;
    @AfterViews
    void afterViews() {
        ArrayAdapter arrayAdapter = ArrayAdapter.createFromResource(this, R.array.apis,
                android.R.layout.simple_list_item_1);
        listView.setAdapter(arrayAdapter);
    }

    @ItemClick(R.id.listView)
    void itemClick(int position) {
        switch (position) {
            case 0:
                showProgress();
                login();
                break;
            case 1:
                HomeAct_.intent(this).withOptions(null).start();
                break;
            case 2:
                ProfileAct_.intent(this).withOptions(null).start();
                break;
            case  3:
                showRet(prefsManager.name().get());
                showRet(String.valueOf(prefsManager.lastUpdated().get()));
                break;
            case 4:
                SignOutAct_.intent(this).withOptions(null).start();
                break;
        }
    }

    @Background
    void login() {
        LoginBean bean = restManager.login(params());
        showRet(bean);
    }

    private Map<String, Object> params() {
        Map<String, Object> ret = new HashMap<>();
        ret.put("username", "tmpa@itm.com");
        ret.put("password", "Deep1234");
        ret.put("loginType", "EM");
        return ret;
    }

    @UiThread
    void showRet(LoginBean bean) {
        dismissProgress();
        tv.setText(bean.toString());
    }

    @UiThread
    void showRet(String name) {
        tv.setText(name);
    }

}
